import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";
class ConsumerReceivePiece extends interfaceUtils{
    static IsRequire=3
    static Name="consumerReceivePiece"
    static TimeStamp=new Date().getTime()
    constructor(consumerId , status,  phone ,offset,limit){
        super()
      
        this.consumerId=consumerId
        this.status=status,         
        this.phone=phone,
        this.offset=offset
        this.limit=limit
        this.timeStamp=new Date().getTime()      
    }
    _getParams(){
       const publicKey=ConsumerReceivePiece.publicKey
       let rsa = new JsEncrypt();
       rsa.setPublicKey(publicKey);
       const { consumerId , status,  phone } =this
       const consumerFrom=ConsumerReceivePiece.consumerFrom      
       const params=JSON.stringify( { consumerId , status,  phone}) 
       const result= rsa.encrypt(params)  
        //    https://www.cnblogs.com/aeolian/p/12133598.html
       return encodeURIComponent(result)      
    }
    _getSign(){
       const key =ConsumerReceivePiece.md5Key+""
       const {phone} =this 
       const consumerFrom=ConsumerReceivePiece.consumerFrom+""
       const {timeStamp}=this
    //    https://blog.csdn.net/wail521/article/details/96430873       
        // const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
        const result = crypto.createHash('md5').update(sign).digest("hex")    
        return result 
    }

    _getData(){
      
        const params=this._getParams()
        const sign=this._getSign()
        const {phone} = this
        const consumerFrom=ConsumerReceivePiece.consumerFrom
        const {timeStamp}=this

        let formData = new FormData();
        formData.append("params",params);
        formData.append("limit", this.limit+"");
        formData.append("offset",this.offset+"");
        formData.append("sign",sign);
        formData.append("phone",phone);
        formData.append("consumerFrom",consumerFrom);
        formData.append("timeStamp",timeStamp);



        return  formData


    }

      /**
     * 
     * @param {keyObj} 将对象排序 变成 字符串
     *  例如 a=1&b=2 
     */
    _parmaSort(keyObj){
        let keyArr = [];
      for (let key in keyObj) {
        keyArr.push(key);
      }
        let reaKryArr = keyArr.sort();
        let newArrStr = "";
        for (let i = 0; i < keyArr.length; i++) {
          newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
        }
    
        let str = newArrStr.substr(0, newArrStr.length - 1);
        return str
    }

}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( qualified(data.consumerId)  &&  qualified(data.status) && qualified(data.phone) ){  
            // switch (class_type.Name) {
            //     case 'register':
            //         if((data.loginPassward+"").length<6 || (data.loginPassward+"").length>20){
            //             Toast.fail({
            //                 message:"请输入6-20位密码"
            //             })
            //             qualify=false
            //         }else{
            //             if((data.confirmPassword+"") ==(data.loginPassward+"")){
            //                 qualify=true
            //             }else{
            //                 qualify=false
            //                 Toast.fail({
            //                     message:"密码不一致"
            //                 })
            //             }
            //         }       
                    

            //      break;               
            // }   
            qualify=true     
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { ConsumerReceivePiece }